package chen.web.forum;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import chen.web.DataSourceProvider;


public class CategoryManagerImpl implements CategoryManager {
	
	private final Category RootCategory = loadRootCategory();
	private static SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(DataSourceProvider.getDataSource());
	
	@Override
	public void add(Category category, Category parent, int position) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(Category category) {
		// TODO Auto-generated method stub
		//不能删除有子栏目的栏目
		if(category.getChildren().size() > 0){
			throw new RuntimeException("不能删除有子栏目的栏目");
		}
		if(category.getParent() == null){
			throw new RuntimeException("不能删除根栏目");
		}
		int pos = position(category);
		category.getParent().getChildren().remove(pos);
		category.setParent(null);
		//未完成 
	}
	
	/**
	 * 计算当前栏目在同级栏目中的位置
	 * @param category
	 * @return	栏目位置, 由0开始. 根栏目返回-1;
	 */
	private int position(Category category){
		//根栏目
		if(category.getParent() == null){
			return -1;
		}
		List<Category> list = category.getParent().getChildren();
		int position = -1;
		for(int i=0; i<list.size(); i++){
			if(list.get(i).getId() == category.getId()){
				position = i;
				break;
			}
		}
		if(position == -1){
			throw new RuntimeException("未知错误");
		}
		return position;
	}

	@Override
	public Category get(int id) {
		// TODO Auto-generated method stub
		return Category.getFromCategory(RootCategory, id);
	}

	@Override
	public Category getByPath(String path) {
		// TODO Auto-generated method stub
		return Category.getFromCategory(RootCategory, path);
	}

	@Override
	public Category getRootCategory() {
		// TODO Auto-generated method stub
		return RootCategory;
	}

	@Override
	public void move(Category category, Category parent, int position) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Category category) {
		// TODO Auto-generated method stub

	}

	
	private static final String SQL_1 = "select * from web_category where cat_id = 1";
	private static Category loadRootCategory(){
		Category root = simpleJdbcTemplate.queryForObject(SQL_1, new CategoryRowMapper());
		_loadRootCategory(root);
		return root;
	}  
	
	private static final String SQL_2 = "select * from web_category where cat_parentId = ? order by cat_order asc";
	private static void _loadRootCategory(Category category){
		List<Category> children = simpleJdbcTemplate.query(SQL_2, new CategoryRowMapper(), category.getId());
		for(Category child : children){
			_loadRootCategory(child);
			child.setParent(category);
			category.addChild(child);
		}
	}

	private static class CategoryRowMapper implements RowMapper<Category>{
		@Override
		public Category mapRow(ResultSet rs, int i) throws SQLException {
			// TODO Auto-generated method stub
			int id = rs.getInt("cat_id");
			Category cat = new Category(id);
			cat.setName(rs.getString("cat_name"));
			cat.setDescription(rs.getString("cat_description"));
			cat.setPath(rs.getString("cat_path"));
			cat.setCheckReply(rs.getBoolean("cat_checkReply"));
			cat.setCheckSubject(rs.getBoolean("cat_checkSubject"));
			cat.setRecycle(rs.getBoolean("cat_recycle"));
			CategoryDynamicInfoManager.loadDynamicInfo(cat);
			return cat;
		}
		
	}

}
